package com.wanmei.controller.order;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmei.commons.ResultUtils;
import com.wanmei.pojo.Address;
import com.wanmei.pojo.OrderProduct;
import com.wanmei.pojo.Result;
import com.wanmei.service.AddressService;
import com.wanmei.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("order")
public class OrderListController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private AddressService addressService;

    /**
     * 获取订单视图
     *
     * @return
     */
    @RequestMapping("orderListView")
    public ModelAndView orderListView(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, String likevalue,ModelAndView model) {


        PageHelper.startPage(pageNum, 4);
        List<OrderProduct> list = orderService.selectOrderByName(likevalue);
        PageInfo pageInfo = new PageInfo(list);
        model.addObject("pageInfo", pageInfo);
        model.addObject("likevalue",likevalue);
        model.setViewName("order/orderList");
        return model;

    }

    /**
     * 获取订单更新视图
     * @param oid
     * @param model
     * @param uid
     * @return
     */
    @RequestMapping("getOrderView")
    public String getOrderView(String oid, Model model, Integer uid) {
        OrderProduct orderProduct = orderService.selectByPrimaryKey(oid);
        List<Address> list = addressService.selectAddressByUid(uid);
        model.addAttribute("address", list);
        model.addAttribute("orderProduct", orderProduct);
        return "order/updateOrder";
    }

    /**
     * 执行订单更新操作
     * @param orderProduct
     * @return
     */

    @RequestMapping("/updateorders")
    @ResponseBody
    public Result updateorders(OrderProduct orderProduct) {
        int i = orderService.updateByPrimaryKeySelective(orderProduct);

        if(i==0){

        return ResultUtils.fail("更新失败",null,0);
        }
        return ResultUtils.success("更新成功",null,0);
    }

    /**
     * 删除订单
     * @param oid
     * @return
     */
    @RequestMapping("/deleteorder")
    @ResponseBody
    public Result deleteorder(@RequestParam(value = "oid") String oid) {
        int i = orderService.deleteByPrimaryKey(oid);
        if(i==0){

            return ResultUtils.fail("更新失败",null,0);
        }
        return ResultUtils.success("更新成功",null,0);
    }


}
